//
//  DHomePageViewController.m
//  项目B 重构腕表
//
//  Created by dllo on 15/12/22.
//  Copyright © 2015年 GYD. All rights reserved.
//

#import "DHomePageViewController.h"
#import "HomePageCollectionViewCell.h"
#import "HomePageModel.h"
#import "DHomePageTwoViewController.h"
#import "DHomePageDetailViewController.h"
#import <Masonry.h>
#import "AppTools.h"
#import "AppDelegate.h"

@interface DHomePageViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong)UICollectionView *collectionView;

@end






@implementation DHomePageViewController


//为写夜间模式 做准备 所以 在这里写左右两个button
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"05.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(rightButtonAction:)];
    
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"05.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ] style:UIBarButtonItemStyleDone target:self action:@selector(leftButtonAction:)];
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"iDailyWATCH";
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    flowLayout.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
    
    flowLayout.minimumLineSpacing = 0;
    
    flowLayout.minimumInteritemSpacing = 0;
    
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flowLayout];
//    self.collectionView.collectionViewLayout = flowLayout;
    //按页翻
    self.collectionView.pagingEnabled = YES;
    //代理
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    //背景颜色
    self.collectionView.backgroundColor = [UIColor whiteColor];
    //滑动方向
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    //添加
    [self.view addSubview: self.collectionView];
    
    //初始化装模型的数组
    self.bigArray = [NSMutableArray array];
    
    //注册cell
    [self.collectionView registerClass:[HomePageCollectionViewCell class] forCellWithReuseIdentifier:@"CollectionViewCell_pe"];
    
    //网络请求地址
    NSString *brandUrl = [NSString stringWithFormat:@"http://watch-cdn.idailywatch.com/api/list/cover/zh-hans?page=1&ver=iphone&app_ver=10"];
    //网络请求
    [AppTools getDataWithURL:brandUrl Block:^(id result) {
        
        NSMutableArray *bigArray = result;
        
        for (NSMutableDictionary *dic in bigArray) {
            HomePageModel *model = [[HomePageModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.bigArray addObject:model];
        }
        //刷新数据
        [self.collectionView reloadData];
    }];
    
}

//右侧按钮 跳到collection瀑布流页面
- (void)rightButtonAction:(UIBarButtonItem *)btn
{
    DHomePageTwoViewController *two = [[DHomePageTwoViewController alloc] init];
    two.allArray = [NSMutableArray arrayWithArray:self.bigArray];
    [self.navigationController pushViewController:two animated:YES];
}

- (void)leftButtonAction:(UIBarButtonItem *)btn
{
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
    if (tempAppDelegate.LeftSlideVC.closed) {
        [tempAppDelegate.LeftSlideVC openLeftView];
    }
    else
    {
        [tempAppDelegate.LeftSlideVC closeLeftView];
    }
}

//设置item
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.bigArray.count;
}
//设置区
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

//cell显示
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"CollectionViewCell_pe";
    HomePageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    HomePageModel *model = [self.bigArray objectAtIndex:indexPath.item];
    cell.model = model;
    return cell;
}


//选中跳到详情页
 - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DHomePageDetailViewController *detail = [[DHomePageDetailViewController alloc] init];
    HomePageModel *model = [self.bigArray objectAtIndex:indexPath.item];
    detail.model = model;
    
    [self.navigationController pushViewController:detail animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
